﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using System.Text;
using BOHVote.Common;

namespace BOH.Interview
{
    public partial class SelectQuestion : System.Web.UI.Page
    {
        public Cmp Cmp = new Cmp();
        public string JobRankLevel = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                if (Request.QueryString["ApplyID"] != null)
                {
                    ShowApplyQuestiongs(Request.QueryString["ApplyID"].ToString());
                }
            //}
        }

        public void ShowApplyQuestiongs(string strApplyID)
        {
            DataSet dst = ApplyClass.getApplyByID(strApplyID);
            if (dst.Tables[0].Rows.Count > 0)
            {
                labApplyName.Text = dst.Tables[0].Rows[0]["Apply_Name"].ToString();
                labUnit.Text = dst.Tables[0].Rows[0]["UnitGovernName"].ToString();
                labSubject.Text = dst.Tables[0].Rows[0]["SubjectName"].ToString();
                labPrintID.Text = dst.Tables[0].Rows[0]["ApplyPrintID"].ToString();
                labSpeciality.Text = dst.Tables[0].Rows[0]["SpecialityID"].ToString();
                labJobRank.Text = dst.Tables[0].Rows[0]["JobRankName"].ToString();
                JobRankLevel = OtherClass.getJobRankLevelByJobRanK(dst.Tables[0].Rows[0]["JobRankID"].ToString());
                
                //get common qies
                string strTestModule = SysConfigClass.getTestModuleBySubjectID(dst.Tables[0].Rows[0]["SubjectID"].ToString());
                if (string.IsNullOrEmpty(strTestModule))
                    strTestModule = "1";
                hiddTestModule.Value = strTestModule.Trim();
                DataSet dstQuestionGT = InterviewClass.getQuestions(string.Empty, dst.Tables[0].Rows[0]["SubjectID"].ToString(), "0", dst.Tables[0].Rows[0]["JobRankID"].ToString(), "0", "A", JobRankLevel, (string)Session["ApplyPeriod"]);
                ShowQuestion(dstQuestionGT, labListQuestionsGT);
                BindSpeciality(dst.Tables[0].Rows[0]["SubjectID"].ToString());
                //if (JobRankLevel == "9")
                //    JobRankID = "0";
                //else if (JobRankLevel == "6")
                //    JobRankID = "1";
                //DataSet dstQuestionS = InterviewClass.getQuestions(string.Empty, dst.Tables[0].Rows[0]["SubjectID"].ToString(), dst.Tables[0].Rows[0]["SpecialityID"].ToString(), dst.Tables[0].Rows[0]["JobRankID"].ToString(), "0", "A", JobRankLevel, (string)Session["ApplyPeriod"]);
                //ShowQuestion(dstQuestionS, labListQuestionsS);
            }
        }

        public void BindSpeciality(string strsubjectID)
        {
            //string strTestModule = SysConfigClass.getTestModuleBySubjectID(strsubjectID);
            //hiddTestModule.Value = strTestModule.Trim();
            repSpecialitys.DataSource = Subject.getAllTestSpecialityBySubjectID(strsubjectID);//Subject.getAllSpecialityBySubjectID(strsubjectID);
            repSpecialitys.DataBind();
        }


        public void ShowQuestion(DataSet dst,Label col)
        {
            string strOnclick = string.Empty;
            List<string> list = new List<string>();
            
            //if (hiddTestModule.Value == "2")
            //{
            //    strOnclick = "onclick=\"notSelect(this)\"";
            //    //strHiddenField = "<input";
            //}
            //else
            //{
            //    strOnclick = "onclick=\"reload(this)\"";
            //}
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                list.Add(dr["QID"].ToString());
            }
            //if (hiddTestModule.Value == "2")
            //{
            //    strOnclick = "onclick=\"notSelect(this)\"";
            //    //strHiddenField = "<input";
            //}
            //else
            //{
            //    BOHVote.Common.common.shuffle(list, Cmp);
            //    strOnclick = "onclick=\"reload(this)\"";
            //}
            //BOHVote.Common.common.shuffle(list, Cmp);
            strOnclick = "onclick=\"reload(this)\"";
            if (hiddTestModule.Value != "2")
                BOHVote.Common.common.shuffle(list, Cmp);

            if (list.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                sb.Append(" <tr>");
                int CountQuestions = list.Count;
                for(int i = 0;i<CountQuestions; i++)
                {
                    string strhiddenField = "<input id='hidden_" + list[i] + "' type='hidden' value=''/>";
                    string subSpeciality = string.Empty;//(string)dst.Tables[0].Select(" QID ='" + list[i] + "' ")[0]["Question_Specialty"];
                    if (!string.IsNullOrEmpty(subSpeciality))
                        subSpeciality = "[" + subSpeciality + "]";
                    string QIDCanUse = InterviewClass.getQuestionCanUsebyQID(list[i].ToString());
                    //if (QIDCanUse.Trim() == "2")
                    //{
                    //    strOnclick = "onclick=\"notSelect(this)\"";
                    //    //strHiddenField = "<input";
                    //}
                    //else
                    //{
                    //    strOnclick = "onclick=\"reload(this)\"";
                    //}
                    if (i % 6 != 0)
                    {
                        if (QIDCanUse.Trim() == "2" && hiddTestModule.Value == "2")
                            sb.Append("     <td style=\"text-align:left; font-size:40px; font-weight:bold;color:red\">" + strhiddenField + "<a id='" + list[i] + "' href=# onclick='CanUseMessage(this)' style=\"color:#aaaaaa\">" + (i + 1) + "&nbsp;" + subSpeciality + "</a></td>  ");
                        else
                            sb.Append("     <td style=\"text-align:left; font-size:40px; font-weight:bold;\">" + strhiddenField + "<a id='" + list[i] + "' href=\"Showquestion.aspx?QID=" + list[i] + "&ApplyID=" + Request.QueryString["ApplyID"].ToString() + "&Mod=" + hiddTestModule.Value + "\"  target=\"_blank\" " + strOnclick + " style=\"color:blue\">" + (i + 1) + "&nbsp;" + subSpeciality + "</a></td>");//<!--<a href=\"Showquestion.aspx?QID=" + list[i] + "&ApplyID=" + Request.QueryString["ApplyID"].ToString() + "\"  target=\"_blank\" " + strOnclick + " >" + (i + 1) + "&nbsp;" + subSpeciality + "</a>-->
                            
                    }
                    else
                    {
                        sb.Append("</tr><tr>");
                        if (QIDCanUse.Trim() == "2" && hiddTestModule.Value == "2")
                            sb.Append("     <td style=\"text-align:left; font-size:40px; font-weight:bold;\">" + strhiddenField + "<a id='" + list[i] + "' href=# onclick=\"CanUseMessage(this)\" style=\"color:#aaaaaa\">" + (i + 1) + "&nbsp;" + subSpeciality + "</a></td>  ");
                        else
                            sb.Append("     <td style=\"text-align:left; font-size:40px; font-weight:bold;\">" + strhiddenField + "<a id='" + list[i] + "' href=\"Showquestion.aspx?QID=" + list[i] + "&ApplyID=" + Request.QueryString["ApplyID"].ToString() + "&Mod=" + hiddTestModule.Value + "\"  target=\"_blank\" " + strOnclick + " style=\"color:blue\">" + (i + 1) + "&nbsp;" + subSpeciality + "</a></td>");//<a href=\"Showquestion.aspx?QID=" + list[i] + "&ApplyID=" + Request.QueryString["ApplyID"].ToString() + "\"  target=\"_blank\" " + strOnclick + " >" + (i + 1) + "&nbsp;" + subSpeciality + "</a>
                    }
                }
                sb.Append(" </tr>");
                sb.Append("</table>");
                col.Text += sb.ToString();
            }
        }

        protected void repSpecialitys_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string JobRankID = string.Empty;
            if (JobRankLevel == "9")
                JobRankID = "0";
            else if (JobRankLevel == "6")
                JobRankID = "1";
            HiddenField hiddSpecialityID = (HiddenField)e.Item.FindControl("hiddSpecialityID");
            HiddenField hiddSubjectID = (HiddenField)e.Item.FindControl("hiddSubjectID");
             Label labListQuestionsS = (Label)e.Item.FindControl("labListQuestionsS");
            DataSet dstQuestionS = InterviewClass.getQuestions(string.Empty, hiddSubjectID.Value, hiddSpecialityID.Value, JobRankID, "", "A", JobRankLevel, (string)Session["ApplyPeriod"]);

            ShowQuestion(dstQuestionS, labListQuestionsS);
        }
    }
}
